import request from '@/utils/request';
import { API_PATH_PRE } from '@/generic/ConfigParams';

export async function meInfo(params) {
  
  return request('/me', {
    headers: {
      'Authorization': `Bearer ${localStorage.getItem("token")}`,
    },
  });
}
export async function queryUsers(params = {}, parse = false) {
  const api_url = `${API_PATH_PRE}user${params.id ? `/${params.id}` : ''}`;
  console.log("===>> queryUsers", api_url)
  return request(api_url, {
    headers: {
      'Authorization': `Bearer ${localStorage.getItem("token")}`,
    },
    parseResponse: parse,
  })
}
export async function addUser(params = {}, parse =false) {

  const api_url = `${API_PATH_PRE}user`;
  console.log("===>> addUser", api_url, params)
  if (params.username && params.password) {

    return request(api_url, {
      method: "POST",
      headers: {
        'Authorization': `Bearer ${localStorage.getItem("token")}`,
      },
      data: {
        'username': params.username,
        'password': params.password,
        'lockPassword': params.lockPassword || false,
        'nickName': params.nickName || '',
        'email': params.email || '',
        'cl': params.cl || 1,
        'enable': params.enable || false,
        'isSuper': params.isSuper || false,
      },
      parseResponse: parse,
    });
  }
  
  return false;
}
export async function deleteUser(params = {}, parse = false) {
  // 批量用户删除不知道做没做？
  if (params.id) {
    const api_url = `${API_PATH_PRE}user/${params.id}`;
    console.log("===>> queryUsers", api_url)
    return request(api_url, {
      method: "DELETE",
      headers: {
        'Authorization': `Bearer ${localStorage.getItem("token")}`,
      },
      parseResponse: parse,
    })
  }

  return false;
}
export async function updateUser(params = {}, parse = false) {
  // 批量用户删除不知道做没做？
  if (params.id) {
    const api_url = `${API_PATH_PRE}user/${params.id}`;
    console.log("===>> queryUsers", api_url)
    console.log("===>> patch", params.patch)
    return request(api_url, {
      method: "PATCH",
      headers: {
        'Authorization': `Bearer ${localStorage.getItem("token")}`,
        'Content-Type': "application/json; charset=UTF-8",
      },
      data: { ...params.patch },
      parseResponse: parse,
    })
  }

  return false;
}


export async function query() {
  return request('/api/users');
}
export async function queryCurrent() {
  return request('/api/currentUser');
}
export async function queryNotices() {
  return request('/api/notices');
}
